global g_root "C:\Users\Hongye Guo\Dropbox (Personal)\Density\Code\Production\Submission"

use "$g_root\Data\EarningsAnnAC_ccm_202106_final.dta", clear
* Data incomplete for the last month. Drop them
drop if mofd(rdq)==ym(2021, 11)
drop lpermno
duplicates drop
rename *, lower

* File containing the imputed earnings announcement day using DellaVigna & Pollet's algorithm
merge 1:1 gvkey datadate using "$g_root\Data\rdq_adj.dta", keep(1 3) nogen
gen rdq_final = rdq_adj
* Backstop with compustat announcement day if unavailable
replace rdq_final = rdq if mi(rdq_final)
drop rdq_adj rdq
rename rdq_final rdq

gen mkval = prccq * cshoq
* Note the requirement of rdq
drop if mi(rdq) | mi(ibq) | mi(ceqq) | mi(mkval)
gen wgt = ceqq
* Cases with negative book value of equity gets a zero weight
replace wgt = 0 if wgt<0

gen rpt_lag = rdq - datadate
gen ym = mofd(rdq)

* Data before 1971Q3 are too thin and cannot form reliable cross sections
drop if ym<ym(1971, 10)
* Earnings announced before a FQ ends have a high chance of being erroneous
drop if rpt_lag <= 0

* FQ end month
gen moy_rdq = month(rdq)
gen moy_datadate = month(datadate)
gen moy_datadate_mod3 = mod(moy_datadate, 3)
gen moy_rdq_mod3 = mod(moy_rdq, 3)

* Table 1: Panel A
tab moy_datadate_mod3 

* Table 1: Panel B
tab moy_rdq_mod3 
tab moy_rdq_mod3 if rpt_lag <= 92 & mod(moy_datadate, 3)==0

*save "$g_root\Data\temp.dta", replace

* Monthly aggregates 
sort ym
by ym: gen ym_count = _N
by ym: gen ym_flag = _n==1

by ym: egen ym_niq = total(niq)
by ym: egen ym_ibq = total(ibq)
by ym: egen ym_ceqq = total(ceqq)
by ym: egen ym_mkval = total(mkval)
gen ym_roe = ym_ibq/ym_ceqq

keep if ym_flag
sort ym
forvalues i = 1/12{
	gen ym_roe_l`i'm = ym_roe[_n-`i']
	gen ym_ceqq_l`i'm = ym_ceqq[_n-`i']
}

* month is the month of the depvar
* am1_flag turns on when the depvar is the first month of a quarter
gen month = month(dofm(ym+1))
gen am1_flag = mod(month,3)==1

* De-seasonalize
* It makes little difference whether this is done, but conceptually it is good to do
forvalue i = 1/12{
	gen month`i'_flag = month == `i'
}
reg ym_roe month*_flag, noc
predict ym_roe_month_mean
replace ym_roe = ym_roe - ym_roe_month_mean

gen ym_roe_l0m = ym_roe
forvalues i = 1/4{
	local j = (`i'-1)*3 
	local jp1 = `j' + 1
	local jp2 = `j' + 2
	foreach l_var in ym_roe{
		gen `l_var'_prevq1_l`i'q = `l_var'_l`j'm
		replace `l_var'_prevq1_l`i'q = `l_var'_l`jp1'm if mod(month,3)==0
		replace `l_var'_prevq1_l`i'q = `l_var'_l`jp2'm if mod(month,3)==1
	}
}

* Convert the data to E/L frequency. E is first month of a quarter. L is the last 2 combined. 
tsset ym
gen ym_roe_f1m = F1.ym_roe
gen ym_roe_f2m = F2.ym_roe
gen ym_ceqq_f1m = F1.ym_ceqq
gen ym_ceqq_f2m = F2.ym_ceqq
gen ym_roe_cw_ft2m = (ym_roe_f1m*ym_ceqq_f1m + ym_roe_f2m*ym_ceqq_f2m)/(ym_ceqq_f1m+ym_ceqq_f2m)

drop if mod(month, 3)==0
sort ym

* Table B1
local col_count = 0
clear matrix

reg ym_roe_cw_ft2m ym_roe_prevq1_l1q if !am1_flag & mod(month, 3)==2, robust 
local col_count = `col_count' + 1
estimates store roe_chg_`col_count'	

reg ym_roe_f1m ym_roe_prevq1_l1q if am1_flag, robust 
local col_count = `col_count' + 1
estimates store roe_chg_`col_count'	
reg ym_roe_cw_ft2m ym_roe_prevq1_l2q if mod(month, 3)==2, robust 
local col_count = `col_count' + 1
estimates store roe_chg_`col_count'	

reg ym_roe_f1m ym_roe_prevq1_l2q if am1_flag, robust 
local col_count = `col_count' + 1
estimates store roe_chg_`col_count'	
reg ym_roe_cw_ft2m ym_roe_prevq1_l3q if mod(month, 3)==2, robust 
local col_count = `col_count' + 1
estimates store roe_chg_`col_count'	

reg ym_roe_f1m ym_roe_prevq1_l3q if am1_flag, robust 
local col_count = `col_count' + 1
estimates store roe_chg_`col_count'	
reg ym_roe_cw_ft2m ym_roe_prevq1_l4q if mod(month, 3)==2, robust 
local col_count = `col_count' + 1
estimates store roe_chg_`col_count'	

reg ym_roe_f1m ym_roe_prevq1_l4q if am1_flag, robust 
local col_count = `col_count' + 1
estimates store roe_chg_`col_count'	

local l_date : display %tdCYND date(c(current_date), "DMY")
local l_time = substr(c(current_time), 1, 2) + substr(c(current_time), 4, 2)
esttab roe_chg_* using "$g_root\Output\ym_roe_us_el_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace 
